DM 'CLEAR LOG; CLEAR OUTPUT;';
libname duration 'path to be completed path to duration data sets see program on durations';



/*************Stack all three data sets containing only effects of wage agreements at firm- or industry levels + end and beginning date for all firms*/
/*period 1994-2001*/

/*duree_effet_br9401 : contains observed durations between two industry-level effects*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
ann_eff: year of agreemente effect
code_dept: local departement
mois_eff: month of agreement effect
t_ents_yp: size class
*/

/*duree_eff_ents9401 : contains observed durations between two firm-level effects*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
ann_eff: year of agreement
code_dept: local departement
mois_eff: month of agreement
t_ents_yp: size class*/

/*duree_eff_debfin9401 : contains dates of beginning and end of sample firms*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
code_dept: local departement
t_ents_yp: size class
indic_deb date of first observation of the firm in the sample
indic_fin date of last observation of the firm in the sample
*/ 



data rev.duree_effet_br_ents9401;
set rev.duree_effet_br9401 rev.duree_eff_ents9401 rev.duree_eff_debfin9401; 
drop an l_an l_mois;
run;
proc sort data=rev.duree_effet_br_ents9401;
by siren ann_eff mois_eff ;
run;
/*define censoring*/
data rev.duree_effet_br_ents9401;
set rev.duree_effet_br_ents9401;
indic_cd=0;
indic_cg=0;
if indic_deb=1 then indic_cg=1;
if indic_fin=1 then indic_cd=1;
run;
proc contents data=rev.duree_effet_br_ents9401 short;
run;
 data rev.duree_effet_br_ents9401;
set rev.duree_effet_br_ents9401;
keep D23 SIREN ann_eff code_dept indic_cd indic_cg  mois_eff t_ents_yp yp; 
if yp=. then delete;run;


proc sort data=rev.duree_effet_br_ents9401 nodupkeys;
by siren ann_eff mois_eff ;
run;

/*compute durations (in months) between two successive effects of agreements industry or firm-level agreeements*/
data rev.duree_effet_br_ents9401;
set rev.duree_effet_br_ents9401;
l_an= lag(ann_eff);
l_mois= lag(mois_eff);
l_siren= lag(siren);
l_cg= lag(indic_cg);
if l_siren=siren then duree=(ann_eff-l_an)*12+mois_eff-l_mois;
if l_siren=siren and l_cg=1 then indic_cg=2;
l2_an= lag(l_an);
run;




/******************************WITH Min WAGE**************/

/*************Stack all 4 data sets containing only effects of wage agreements at firm- or industry levels + NMW */
data rev.duree_effets_br_ents_smic9401_1;
set rev.duree_effet_br9401 rev.duree_eff_ents9401 rev.duree_eff_debfin9401 rev.duree_effet_smic9401; 
drop an l_an l_mois;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_1;
by siren ann_eff mois_eff ;
run;
data rev.duree_effets_br_ents_smic9401_1;
set rev.duree_effets_br_ents_smic9401_1;
indic_cd=0;
indic_cg=0;
if indic_deb=1 then indic_cg=1;
if indic_fin=1 then indic_cd=1;
run;


data rev.duree_effets_br_ents_smic9401_1;
set rev.duree_effets_br_ents_smic9401_1;
drop l_siren ;
run;


/****************************************************************/
/* We duplicate the whole data set creating two other data sets:
/* - one accounting for minimum wage earners: the data set will contain the proportion of NMW earners in a given industry 
for a given date and will be associated with all firms. We then will calculate the nb of employees in a given firm covered by NMW
	- one accounting for other workers: the data set will contain the proportion of other workers (100-%of NMW earners) in a given industry 
for a given date and will be associated with all firms. We then will calculate the nb of employees in a given firm not covered by NMW

Then we will stack the data sets and compute the weighted (by nb of employees) durations of effects of wage agreements including NMW*/

/*duration.duree_effet_smic9401_1: contains for each firm*year the % of minimum wage earners in its industry * d�partement (geographical location)***/
data rev.duree_effet_smic9401_1;
set rev.duree_effet_smic9401_1;
rename an=ann_eff;
run;
proc sort data=rev.duree_effet_smic9401_1;
by siren ann_eff;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_1;
by siren ann_eff;
run;
data rev.duree_effets_br_ents_smic9401_1b;
merge rev.duree_effets_br_ents_smic9401_1 rev.duree_effet_smic9401_1;
by siren ann_eff;
run;
data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
l_an=lag(ann_eff);
l_siren=lag(siren);
l_psmic2=lag(p_smic2);
l_yp=lag(yp);
if l_an=ann_eff and l_siren=siren and p_smic2=. then p_smic2=l_psmic2;
if l_an=ann_eff and l_siren=siren and yp=. then yp=l_yp;

l2_an=lag(lag(ann_eff));
l2_siren=lag(lag(siren));
l2_psmic2=lag(lag(p_smic2));
l2_yp=lag(lag(yp));
if l2_an=ann_eff and l2_siren=siren and p_smic2=. then p_smic2=l2_psmic2;
if l2_an=ann_eff and l2_siren=siren and yp=. then yp=l2_yp;

run;
data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
drop l_an l2_an l_siren l2_siren  l_psmic2 l2_psmic2 nes114;
run;

proc sort data=rev.duree_effets_br_ents_smic9401_1b;
by siren descending ann_eff descending mois_eff;
run;
data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
l_an=lag(ann_eff);
l_siren=lag(siren);
l_psmic2=lag(p_smic2);
l_yp=lag(yp);
if l_an=ann_eff and l_siren=siren and p_smic2=. then p_smic2=l_psmic2;
if l_an=ann_eff and l_siren=siren and yp=. then yp=l_yp;

l2_an=lag(lag(ann_eff));
l2_siren=lag(lag(siren));
l2_psmic2=lag(lag(p_smic2));
l2_yp=lag(lag(yp));
if l2_an=ann_eff and l2_siren=siren and p_smic2=. then p_smic2=l2_psmic2;
if l2_an=ann_eff and l2_siren=siren and yp=. then yp=l2_yp;
run;

data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
drop l_an l_siren l_psmic2;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_1b;
by siren ann_eff mois_eff;
run;

data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
drop l_an l2_an l_siren l2_siren  l_psmic2 l2_psmic2 nes114 indic_deb indic_fin;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_1b nodupkeys;
by siren ann_eff mois_eff ;
run;

/*compute durations (in months) between two successive effects of agreements industry or firm-level agreeements*/
/* for workers covered by NMW   *****************************************************************/
data rev.duree_effets_br_ents_smic9401_1b;
set rev.duree_effets_br_ents_smic9401_1b;
l_an= lag(ann_eff);
l_mois= lag(mois_eff);
l_siren= lag(siren);
l_cg= lag(indic_cg);
if l_siren=siren then duree=(ann_eff-l_an)*12+mois_eff-l_mois;
if l_siren=siren and l_cg=1 then indic_cg=2;
run;




/*workers not covered by NMW ***/
data rev.duree_effets_br_ents_smic9401_0;
set rev.duree_effet_br9401 rev.duree_eff_ents9401 rev.duree_eff_debfin9401; 
drop an l_an l_mois;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_0;
by siren ann_eff mois_eff ;
run;
data rev.duree_effets_br_ents_smic9401_0;
set rev.duree_effets_br_ents_smic9401_0;
indic_cd=0;
indic_cg=0;
if indic_deb=1 then indic_cg=1;
if indic_fin=1 then indic_cd=1;
run;
data rev.duree_effets_br_ents_smic9401_0;
set rev.duree_effets_br_ents_smic9401_0;
drop l_siren indic_deb indic_fin;
run;
data rev.duree_effet_smic9401_0;
set rev.duree_effet_smic9401_0;
rename an=ann_eff;
run;
proc sort data=rev.duree_effet_smic9401_0;
by siren ann_eff;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_0;
by siren ann_eff;
run;
data rev.duree_effets_br_ents_smic9401_0b;
merge rev.duree_effets_br_ents_smic9401_0 rev.duree_effet_smic9401_0;
by siren ann_eff;
run;
data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
l_an=lag(ann_eff);
l_siren=lag(siren);
l_psmic2=lag(p_smic2);
l_yp=lag(yp);
if l_an=ann_eff and l_siren=siren and p_smic2=. then p_smic2=l_psmic2;
if l_an=ann_eff and l_siren=siren and yp=. then yp=l_yp;

l2_an=lag(lag(ann_eff));
l2_siren=lag(lag(siren));
l2_psmic2=lag(lag(p_smic2));
l2_yp=lag(lag(yp));
if l2_an=ann_eff and l2_siren=siren and p_smic2=. then p_smic2=l2_psmic2;
if l2_an=ann_eff and l2_siren=siren and yp=. then yp=l2_yp;
run;
data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
drop l_an l2_an l_siren l2_siren  l_psmic2 l2_psmic2 nes114 indic_deb indic_fin;
run;

proc sort data=rev.duree_effets_br_ents_smic9401_0b;
by siren descending ann_eff descending mois_eff;
run;
data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
l_an=lag(ann_eff);
l_siren=lag(siren);
l_psmic2=lag(p_smic2);
l_yp=lag(yp);
if l_an=ann_eff and l_siren=siren and p_smic2=. then p_smic2=l_psmic2;
if l_an=ann_eff and l_siren=siren and yp=. then yp=l_yp;

l2_an=lag(lag(ann_eff));
l2_siren=lag(lag(siren));
l2_psmic2=lag(lag(p_smic2));
l2_yp=lag(lag(yp));
if l2_an=ann_eff and l2_siren=siren and p_smic2=. then p_smic2=l2_psmic2;
if l2_an=ann_eff and l2_siren=siren and yp=. then yp=l2_yp;
run;
data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
drop l_an l_siren l_psmic2;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_0b;
by siren ann_eff mois_eff;
run;

data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
drop l_an l2_an l_siren l2_siren  l_psmic2 l2_psmic2 nes114 indic_deb indic_fin;
run;
proc sort data=rev.duree_effets_br_ents_smic9401_0b nodupkeys;
by siren ann_eff mois_eff;
run;
data rev.duree_effets_br_ents_smic9401_0b ;
set rev.duree_effets_br_ents_smic9401_0b ;
if mois_eff=. then delete ;
run;



/*compute durations (in months) between two successive agreements industry or firm-level agreeements*/
/* for workers NOT covered by NMW   *****************************************************************/
data rev.duree_effets_br_ents_smic9401_0b;
set rev.duree_effets_br_ents_smic9401_0b;
l_an= lag(ann_eff);
l_mois= lag(mois_eff);
l_siren= lag(siren);
l_cg= lag(indic_cg);
if l_siren=siren then duree=(ann_eff-l_an)*12+mois_eff-l_mois;
if l_siren=siren and l_cg=1 then indic_cg=2;

run;



/************STACK coverzed and non-covered by NMW workers*/
data rev.duree_eff_br_ents_smic9401_10b;
set rev.duree_effets_br_ents_smic9401_0b rev.duree_effets_br_ents_smic9401_1b;
yp_smic=yp*p_smic2/100;
run;
data rev.duree_eff_br_ents_smic9401_10b;
set rev.duree_eff_br_ents_smic9401_10b;
drop l2_an l_an l_cg l_mois l_siren ; 
run;


/******************************COMPUTE Descriptive statistics on durations*********************/
/*duration between two effects of wage agreements (weighted by number of employees)*/
/*TABLE 1 line 2*/
proc univariate data=rev.duree_eff_br_ents_smic9401_10b (where=(indic_cg=0 and indic_cd=0));
var duree;
weight yp_smic;
run;
proc freq data=rev.duree_eff_br_ents_smic9401_10b (where=(indic_cg=0 /*and indic_cd=0*/ ));
tables duree ;
weight yp_smic;
run;
quit;
/* Density function and Hazard rate Figure 2*/
proc lifetest data=rev.duree_eff_br_ents_smic9401_10b (where=(indic_cg=0 )) plots=(h s p) outsurv=rev.res_dur_effet_smic_9401
intervals=(1 to 40 by 1) method =lt maxtime=50 noprint;
time duree*indic_cd(1);
freq yp_smic;
run;
quit;

